﻿using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using AntDesign;
using Blazor.Core;
using Blazor.Core.Extensions;
using Blazor.Localization;
using Blazor.Pages.UpdateSetting.Modals.Whitelist;
using Blazor.ViewModels.UpdateSetting;
using Microsoft.Extensions.Localization;
using ReactiveUI;

namespace Blazor.Views.UpdateSetting;

public partial class WhitelistRow : SoraniComponentBase<WhitelistEntryViewModel>
{
    private readonly IStringLocalizer<UpdateSettingLocalization> _loc;
    private readonly ModalService _modalService;

    private readonly TypographyEditableConfig _rawEditConfig;
    private readonly TypographyEditableConfig _defaultEditConfig;
    private readonly TypographyEditableConfig _commentEditConfig;

    public WhitelistRow(IStringLocalizer<UpdateSettingLocalization> loc, ModalService modalService)
    {
        _loc = loc;
        _modalService = modalService;

        _rawEditConfig = new()
        {
            OnChange = EditRawHandler,
        };
        _defaultEditConfig = new()
        {
            OnChange = EditDefaultHandler,
        };
        _commentEditConfig = new()
        {
            OnChange = EditCommentHandler,
        };
    }

    private async void EditCommentHandler(string s)
    {
        if (ViewModel is null)
        {
            return;
        }

        var success = await ViewModel.UpdateComment.Execute(s);
        if (success)
        {
            StateHasChanged();
        }
    }

    private async void EditDefaultHandler(string s)
    {
        if (ViewModel is null)
        {
            return;
        }

        var success = await ViewModel.UpdateDefault.Execute(s);
        if (success)
        {
            StateHasChanged();
        }
    }

    private async void EditRawHandler(string s)
    {
        if (ViewModel is null)
        {
            return;
        }

        var success = await ViewModel.UpdateRaw.Execute(s);
        if (success)
        {
            StateHasChanged();
        }
    }

    protected override void BindWithViewModel(WhitelistEntryViewModel viewModel, CompositeDisposable disposable)
    {
        viewModel.ModifyResourceVersionEntryInteraction.RegisterHandler(ModifyResourceVersionEntryInteractionHandler).DisposeWith(disposable);
        viewModel.ConfirmDeleteInteraction.RegisterHandler(ConfirmDeleteInteractionHandler).DisposeWith(disposable);
        viewModel.ConfirmApplyInteraction.RegisterHandler(ConfirmApplyInteractionHandler).DisposeWith(disposable);
    }
    
    private async Task ModifyResourceVersionEntryInteractionHandler(IInteractionContext<WhitelistOptions, WhitelistResult?> interactionContext)
    {
        var result = await _modalService.OpenCustomModal<WhitelistModal, WhitelistOptions, WhitelistResult?>(interactionContext.Input);
        
        interactionContext.SetOutput(result);
    }

    private async Task ConfirmDeleteInteractionHandler(IInteractionContext<Unit, bool> interactionContext)
    {
        var result = await _modalService.ConfirmAsync(new ConfirmOptions()
        {
            Content = _loc[UpdateSettingLocalization.DeleteWhitelistEntryTip].ToString()
        });

        interactionContext.SetOutput(result);
    }
    
    private async Task ConfirmApplyInteractionHandler(IInteractionContext<Unit, bool> interactionContext)
    {
        var result = await _modalService.ConfirmAsync(new ConfirmOptions()
        {
            Content = _loc[UpdateSettingLocalization.WhyApplyWhitelistIsDanger].ToString()
        });

        interactionContext.SetOutput(result);
    }

    private async Task ApplyClickHandler()
    {
        if (ViewModel is null)
        {
            return;
        }

        await ViewModel.Apply.Execute();
        await ViewModel.Delete.Execute();
    }
}